﻿using System.Collections;
using System.Collections.Generic;
using System.Data;
using YunZuan.Models.Entities;

namespace Yunzuan.Foundation.DataAccess
{
    /// <summary>
    /// 数据操作接口
    /// </summary>
    public interface IDataContextBase
    {
        void Add(List<object> list);
        /// <summary>
        /// 新增记录
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>是否成功</returns>
        int Add(BaseEntity entity);

        /// <summary>
        /// 新增记录
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="excludeColumns">排除字段</param>
        /// <returns>是否成功</returns>
        int Add(BaseEntity entity, params string[] excludeColumns);

        /// <summary>
        /// 记录是否存在
        /// </summary>
        /// <remarks>
        /// 遍历每个属性，使用AND操作符连接所有查询条件
        /// </remarks>
        /// <param name="entity">查询参数</param>
        /// <returns>是否存在</returns>
        bool IsExist(BaseEntity entity);

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="entity"></param>
        int Delete(BaseEntity entity);

        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="entity"></param>
        int Edit(BaseEntity entity);

        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="excludeColumns">要排除的字段</param>
        /// <returns>是否更新成功</returns>
        int Edit(BaseEntity entity, params string[] excludeColumns);

        /// <summary>
        /// 获取单条记录
        /// </summary>
        /// <remarks>
        /// 根据传入参数的主键获取数据
        /// </remarks>
        /// <typeparam name="T">返回数据类型</typeparam>
        /// <param name="entity">实体</param>
        /// <returns>单条记录</returns>
        T Get<T>(BaseEntity entity) where T : BaseEntity, new();

        /// <summary>
        /// 获取单条记录
        /// </summary>
        /// <typeparam name="T">返回数据类型</typeparam>
        /// <param name="id">数据标识</param>
        /// <returns>单条记录</returns>
        T Get<T>(int? id) where T : BaseEntity, new();

        /// <summary>
        /// 获取单条记录
        /// </summary>
        /// <typeparam name="T">返回数据类型</typeparam>
        /// <param name="id">数据标识</param>
        /// <returns>HashResult</returns>
        HashResult GetHashResult<T>(int? id) where T : BaseEntity;

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="statementName">插入语句ID</param>
        /// <param name="parameterObject">插入参数</param>
        /// <returns>插入语句返回值</returns>
        object Insert(string statementName, object parameterObject);

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="statementName">删除语句ID</param>
        /// <param name="parameterObject">删除参数</param>
        /// <returns>影响的记录数量</returns>
        int Delete(string statementName, object parameterObject);

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="statementName">更新语句ID</param>
        /// <param name="parameterObject">更新参数</param>
        /// <returns>影响的记录数量</returns>
        int Update(string statementName, object parameterObject);

        /// <summary>
        /// 创建查询命令，用于参数化查询
        /// </summary>
        /// <remarks>
        /// 仅用于对性能有极高要求时使用
        /// </remarks>
        /// <returns></returns>
        IDbCommand CreateCommand();

        #region Query

        /// <summary>
        /// 查询泛型字典数据
        /// </summary>
        /// <typeparam name="K">结果Key类型</typeparam>
        /// <typeparam name="V">结果Value类型</typeparam>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="keyProperty">字典Key对应的字段名称</param>
        /// <returns>字典</returns>
        IDictionary<K, V> QueryForDictionary<K, V>(string statementName, object parameterObject, string keyProperty);

        /// <summary>
        /// 查询字典数据
        /// </summary>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="keyProperty">字典Key对应的字段名称</param>
        /// <returns>字典</returns>
        IDictionary QueryForDictionary(string statementName, object parameterObject, string keyProperty);

        /// <summary>
        /// 查询泛型字典数据
        /// </summary>
        /// <typeparam name="K">结果Key类型</typeparam>
        /// <typeparam name="V">结果Value类型</typeparam>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="keyProperty">字典Key对应的字段名称</param>
        /// <param name="valueProperty">字典Value对应的字段名称</param>
        /// <returns>字典</returns>
        IDictionary<K, V> QueryForDictionary<K, V>(string statementName, object parameterObject, string keyProperty, string valueProperty);

        /// <summary>
        /// 查询字典数据
        /// </summary>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="keyProperty">字典Key对应的字段名称</param>
        /// <param name="valueProperty">字典Value对应的字段名称</param>
        /// <returns>字典</returns>
        IDictionary QueryForDictionary(string statementName, object parameterObject, string keyProperty, string valueProperty);

        /// <summary>
        /// 查询列表数据
        /// </summary>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <returns></returns>
        IList QueryForList(string statementName, object parameterObject);

        /// <summary>
        /// 查询泛型列表数据
        /// </summary>
        /// <typeparam name="T">结果类型</typeparam>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <returns>列表数据</returns>
        IList<T> QueryForList<T>(string statementName, object parameterObject);

        /// <summary>
        /// 查询泛型列表数据
        /// </summary>
        /// <typeparam name="T">结果类型</typeparam>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="resultObject">现有列表</param>
        void QueryForList<T>(string statementName, object parameterObject, IList<T> resultObject);

        /// <summary>
        /// 查询列表数据
        /// </summary>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="resultObject">现有列表</param>
        void QueryForList(string statementName, object parameterObject, IList resultObject);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T">返回结果类型</typeparam>
        /// <param name="statementName">语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="pageSize">单页数量</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="countOnlyFirstPage">是否只在查询第一页时统计总数</param>
        /// <returns>分页数据</returns>
        PageList<T> QueryForList<T>(string statementName, object parameterObject, int pageSize, int pageIndex, bool countOnlyFirstPage) where T : class;

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T">返回结果类型</typeparam>
        /// <param name="statementName">语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="pageSize">单页数量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>分页数据</returns>
        PageList<T> QueryForList<T>(string statementName, object parameterObject, int pageSize, int pageIndex) where T : class;

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="statementName">语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="pageSize">单页数量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>分页Hashtable数据</returns>
        PageList<Hashtable> QueryForList(string statementName, object parameterObject, int pageSize, int pageIndex);

        /// <summary>
        /// 查询字典数据
        /// </summary>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="keyProperty">字典Key属性对应字段名称</param>
        /// <returns>字典</returns>
        IDictionary QueryForMap(string statementName, object parameterObject, string keyProperty);

        /// <summary>
        /// 查询字典数据
        /// </summary>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="keyProperty">字典Key属性对应字段名称</param>
        /// <param name="valueProperty">字典Value属性对应字段名称</param>
        /// <returns></returns>
        IDictionary QueryForMap(string statementName, object parameterObject, string keyProperty, string valueProperty);

        /// <summary>
        /// 单条数据查询
        /// </summary>
        /// <typeparam name="T">返回结果类型</typeparam>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <returns>单条数据</returns>
        T QueryForObject<T>(string statementName, object parameterObject);

        /// <summary>
        /// 单条数据查询
        /// </summary>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <returns>object</returns>
        object QueryForObject(string statementName, object parameterObject);

        /// <summary>
        /// 单条数据查询
        /// </summary>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="resultObject">现有对象</param>
        /// <returns>现有对象</returns>
        object QueryForObject(string statementName, object parameterObject, object resultObject);

        /// <summary>
        /// 单条数据查询
        /// </summary>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <param name="resultObject">现有对象</param>
        /// <returns>现有对象</returns>
        T QueryForObject<T>(string statementName, object parameterObject, T instanceObject);

        /// <summary>
        /// 统计总数
        /// </summary>
        /// <param name="statementName">查询语句ID</param>
        /// <param name="parameterObject">查询参数</param>
        /// <returns>数量</returns>
        int Count(string statementName, object parameterObject);

        #endregion
    }
}
